package com.yff.modules.monitor.notify;

import de.codecentric.boot.admin.server.domain.entities.Instance;
import de.codecentric.boot.admin.server.domain.entities.InstanceRepository;
import de.codecentric.boot.admin.server.domain.events.InstanceEvent;
import de.codecentric.boot.admin.server.domain.events.InstanceStatusChangedEvent;
import de.codecentric.boot.admin.server.notify.AbstractStatusChangeNotifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Mono;

/**
 * 通知发送配置
 *
 * @author lw
 */

@Component
public class StatusChangeNotifier extends AbstractStatusChangeNotifier {

    private static final Logger log = LoggerFactory.getLogger(StatusChangeNotifier.class);


    public StatusChangeNotifier(InstanceRepository repository)
    {
        super(repository);
    }

    @Override
    protected Mono<Void> doNotify(InstanceEvent event, Instance instance) {
        return Mono.fromRunnable(() -> {
            if (event instanceof InstanceStatusChangedEvent)
            {
                String status =  ((InstanceStatusChangedEvent) event).getStatusInfo().getStatus();
                switch (status)
                {
                    // 健康检查没通过
                    case "DOWN":
                        log.info("发送 健康检查没通过 的通知！Instance {} ({}) {}"
                                ,instance.getRegistration().getName(), event.getInstance(),
                                event.getType());
                        break;
                    // 服务离线
                    case "OFFLINE":
                        log.info("发送 服务离线 的通知！Instance {} ({}) {}",
                                instance.getRegistration().getName(), event.getInstance(),
                                event.getType());
                        break;
                    // 服务上线
                    case "UP":
                        log.info("发送 服务上线 的通知！Instance {} ({}) {}",
                                instance.getRegistration().getName(), event.getInstance(),
                                event.getType());
                        break;
                    // 服务未知异常
                    case "UNKNOWN":
                        log.info("发送 服务未知异常 的通知！Instance {} ({}) {}",
                                instance.getRegistration().getName(), event.getInstance(),
                                event.getType());
                        break;
                    default:
                        break;
                }
            }
        });
    }
}
